package com.infinum.snacky.default

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Checkbox
import androidx.compose.material3.CheckboxDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.MaterialTheme.colorScheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.fhc.view.R
import com.infinum.snacky.SnackyData
import com.infinum.snacky.SnackyDuration
import com.infinum.snacky.SnackyHostState
import com.infinum.snacky.SnackyState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch


@Composable
fun ShowDoNotReminderAgainSnackbar(
    showState: MutableState<Boolean> = remember { mutableStateOf(false) },
    title: String = stringResource(R.string.tips),
    message: String,
    question:  String = stringResource(R.string.donot_remind_again),
    snackbarHostState: SnackyHostState,
    duration: SnackyDuration = SnackyDuration.Short,
    okAction: (Boolean) -> Unit,
    onDismiss: (() -> Unit)? = null,
) {
    LaunchedEffect(showState) {
        if(showState.value)  launchDoNotReminderAgainSnackbar(
            message = message,
            title = title,
            question = question,
            snackbarHostState = snackbarHostState,
            duration = duration,
            okAction= okAction,
            onDismiss= onDismiss
        )
    }
}



class DoNotReminderAgainData(
    val title: String,
    val message: String,
    val question: String,
    override val duration: SnackyDuration = SnackyDuration.Indefinite,
    val okAction: (Boolean) -> Unit,
    val onDismiss:  (() -> Unit)? = null,

    ) : SnackyData {
    @Composable
    override fun Render(snackbarState: SnackyState) {
        DoNotReminderAgainSnackbar(
            title = title,
            message = message,
            question = question,
            okAction = okAction,
            dismissAction = {if(onDismiss != null) onDismiss.invoke() else snackbarState::dismiss},
        )
    }
}
private fun CoroutineScope.launchDoNotReminderAgainSnackbar(
    title: String ,
    message: String,
    question: String ,
    snackbarHostState: SnackyHostState,
    duration: SnackyDuration = SnackyDuration.Indefinite,
    okAction: (Boolean) -> Unit,
    onDismiss:  (() -> Unit)? = null,
) = this.launch {
    snackbarHostState.showSnackbar(
        DoNotReminderAgainData(
            title = title,
            message = message,
            duration = duration,
            question = question,
            okAction = okAction,
            onDismiss = onDismiss,
        ),
    )
}

@Composable
private fun DoNotReminderAgainSnackbar(
    title: String,
    message: String,
    question: String,
    okText: String = "OK",
    okAction: (Boolean) -> Unit,
    dismissAction: () -> Unit,
) {
    val dnd = remember { mutableStateOf(false) }
    Surface(
        modifier = Modifier.fillMaxWidth().padding(horizontal = 44.dp, vertical = 44.dp),
        shape = RoundedCornerShape(12.dp),
        shadowElevation = 6.dp,
    ) {
        Row(
            modifier = Modifier.padding(top = 22.dp, start = 18.dp, end = 18.dp, bottom = 18.dp),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            Column ( modifier = Modifier.weight(1f)){
                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.Absolute.Left
                ) {
                    Text(
                        modifier = Modifier.width(40.dp),
                        text = "\uD83D\uDC49",
                        fontWeight = FontWeight.Bold,
                        fontSize = 22.sp,
                        color = Color.Black,
                    )
                    Text(
                        text = title.uppercase(),
                        fontWeight = FontWeight.Bold,
                        fontSize = 22.sp,
                        color = Color.Black,
                    )
                }

                Text(
                    modifier = Modifier.padding(start = 40.dp, top = 6.dp),
                    text = message,
                    fontWeight = FontWeight.Bold,
                    color = Color.Gray,
                    fontSize = 16.sp,
                )

                Row(
                    modifier = Modifier.padding(start = 26.dp)
                        .clickable( onClick = {
                            dnd.value = !dnd.value
                        } ),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.Absolute.Left
                ) {
                    Checkbox(
                        modifier = Modifier,
                        checked =dnd.value,
                        onCheckedChange = { dnd.value = it },
                        enabled = true,
                        colors = CheckboxDefaults.colors(
                            checkedColor = colorScheme.secondary,
                            uncheckedColor = Color.DarkGray,
                            checkmarkColor = Color.White
                        )
                    )
                    Text(
                        modifier = Modifier.padding(start = 0.dp),
                        text = question,
                        style = MaterialTheme.typography.bodySmall
                    )
                }

                Spacer(modifier = Modifier.height(4.dp))

                if(okText.isNotEmpty()){
                    Row {
                        Spacer(modifier = Modifier.weight(1f))

                        Box(
                            modifier = Modifier
                                .clickable(onClick = { okAction(dnd.value) })
                                .background(color = Color.Black.copy(alpha = 0.1f),    shape = RoundedCornerShape(4.dp) )
                                .padding(start = 16.dp, top = 8.dp, bottom = 8.dp, end =16.dp) // 外层Box添加8dp内边距
                                .wrapContentSize() )// 根据内容自动调
                        {
                            Text(
                                modifier = Modifier,
                                text = okText,
                                fontWeight = FontWeight.ExtraBold,
                                textAlign = TextAlign.Center,
                                fontSize = 16.sp,
                                color = Color(0xFF006CE0),
                            )
                        }

                    }
                }
            }

            IconButton(
                modifier = Modifier
                    .size(24.dp)
                    .align(Alignment.Top),
                onClick = dismissAction,
            ) {
                Icon(
                    painter = painterResource(id = android.R.drawable.ic_menu_close_clear_cancel),
                    contentDescription = "Dismiss",
                    tint = Color.Black,
                )
            }
        }
    }
}


@Composable
@Preview
fun DoNotReminderAgainSnackbarPreview() {
    DoNotReminderAgainSnackbar (
        title = "title",
        message = "message",
        question = "question",
        okAction = { },
        dismissAction = { },
    )
}
